<?php
namespace app\http\middleware;

use Closure;
use think\Lang;
use think\Request;
use think\App;

class LoadLangPack
{
    protected $app;

    protected $lang;

    public function __construct(App $app, Lang $lang)
    {
        $this->app  = $app;
        $this->lang = $lang;
    }

    public function handle(Request $request, Closure $next)
    {
        $controller = $request->controller();
        $controller = explode('.',$controller);
        $langset = $this->lang->detect($request);
        // 加载当前控制器语音包和公共语言包
        $this->lang->load(
            [
                sprintf(env('root_path').'application\\common\\lang\\%s\\%s\\%s.php', $langset, $request->module(), end($controller)),
                env('root_path').'application\\common\\lang\\'.$langset.'\\Common.php',
                env('root_path').'application\\common\\lang\\'.$langset.'\\ErrorMsg.php'
            ]
        );
        return $next($request);
    }

}